//
//  YearMonthPicker.swift
//  HuaYun
//
//  Created by enghou on 2019/1/11.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit

class YearMonthPicker: UIView,UIPickerViewDelegate,UIPickerViewDataSource {
    internal func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    
    internal func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return self.years.count
        default:
            return 12
        }
    }
    
    internal func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.hex("191919")
        label.textAlignment = .center
        label.height = 30
        switch component {
        case 0:
            label.text = self.years[row]
        default:
            label.text = String(format: "%.2i", row + 1)
        }
        return label
    }
    
    private var years = (2000 ..< 3000).map { (rst) -> String in
        return String(rst)
    }
    
    //yyyy-mm
    var date: String? {
        set{
            if (newValue?.count ?? 0) > 0 {
                if newValue!.count > 4 {
                    let yearstr = newValue!.prefix(4)
                    if let yeardouble = Double.init(yearstr) {
                        if yeardouble > 1999.0 {
                            self.pickerView.selectRow(Int.init(yeardouble) - 2000, inComponent: 0, animated: false)
                        }
                        let monthstr = newValue!.prefix(7).suffix(2)
                        if let month = Double.init(monthstr) {
                            if month > 0 && month < 12 {
                                self.pickerView.selectRow(Int(month) - 1, inComponent: 1, animated: false)
                            }
                        }
                    }
                }
                
            }
        }
        get{
            let year = pickerView.selectedRow(inComponent: 0) + 2000
            let month = pickerView.selectedRow(inComponent: 1) + 1
            return String(year) + "-" + String(format: "%.2i", month)
        }
    }
    
    private let pickerView = UIPickerView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.backgroundColor = UIColor.white
        self.addSubview(pickerView)
        pickerView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalToSuperview()
        }
        let date = Date().dateLineStr
        self.date = String(date.prefix(7))
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.backgroundColor = UIColor.white
        self.addSubview(pickerView)
        pickerView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalToSuperview()
        }
        let date = Date().dateLineStr
        self.date = String(date.prefix(7))
    }
}
